// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Drawing;
using HAKGERSoft;
using Mono.Unix;
using NLog;

namespace wxGadu
{
		
	public class GaduSettingsPanel : wx.Panel
	{
		#region Id
		public enum Id
		{
			uinTC,
//			passwordTC,
			nameTC,
			defaultStatusCombo,
			saveDescriptionCheck,
			automaticServerCheck,
			serverAddressTC,
			connectionTimeoutSpin		
		}
		#endregion
		
		#region sizers
		private wx.BoxSizer topSizer;
		private wx.StaticBoxSizer accountInfoBox;
		private wx.FlexGridSizer accountInfoGrid;
		private wx.StaticBoxSizer statusBox;
		private wx.FlexGridSizer statusGrid;
		private wx.StaticBoxSizer connectionBox;
		private wx.FlexGridSizer connectionGrid;
		#endregion
		
		#region controls
		private wx.StaticText uinST;
		private wx.TextCtrl uinTC;
//		private wx.StaticText passwordST;
//		private wx.TextCtrl passwordTC;
		private wx.StaticText nameST;
		private wx.TextCtrl nameTC;
		private wx.StaticText defaultStatusST;
		private wx.ComboBox defaultStatusCombo;
		private wx.CheckBox saveDescriptionCheck;
		private wx.CheckBox automaticServerCheck;
		private wx.StaticText serverAddressST;
		private wx.TextCtrl serverAddressTC;
		private wx.StaticText connectionTimeoutST;
		private wx.SpinCtrl connectionTimeoutSpin;
		private wx.StaticBox accountInfoSB;
		private wx.StaticBox statusSB;
		private wx.StaticBox connectionSB;
		#endregion
		
		#region data members
#if LOG
		private Logger logger = LogManager.GetCurrentClassLogger();	
#endif
		#endregion
		
		public GaduSettingsPanel(wx.Window parent, int id, Point pos, Size size, uint style):
			base(parent, id, pos, size, style)
		{
			DoControls();
			DoControlsProperites();
			DoLayout();
			
			EVT_UPDATE_UI((int) Id.serverAddressTC, new wx.EventListener(OnAutoServerAddressChange));
		}
		
		private void DoControls()
		{
			uinST = new wx.StaticText(this, Catalog.GetString("GG number:"));
			uinTC = new wx.TextCtrl(this, (int) Id.uinTC, string.Empty);
//			passwordST = new wx.StaticText(this, Catalog.GetString("Password:"));
//			passwordTC = new wx.TextCtrl(this, (int) Id.passwordTC, string.Empty, wx.Window.wxDefaultPosition,
//			                             wx.Window.wxDefaultSize, wx.TextCtrl.wxTE_PASSWORD);
			nameST = new wx.StaticText(this, Catalog.GetString("Name:"));
			nameTC = new wx.TextCtrl(this, (int) Id.nameTC, string.Empty);
			defaultStatusST = new wx.StaticText(this, Catalog.GetString("Set status on start:"));
			defaultStatusCombo = new wx.ComboBox(this, (int) Id.defaultStatusCombo, string.Empty,
			                                     wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize, 
			                                     new string[0], wx.ComboBox.wxCB_READONLY);
			saveDescriptionCheck = new wx.CheckBox(this, (int) Id.saveDescriptionCheck, Catalog.GetString("Save descriptions list:"), 
			                                       wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                       wx.Alignment.wxALIGN_RIGHT);
			automaticServerCheck = new wx.CheckBox(this, (int) Id.automaticServerCheck, Catalog.GetString("Auto select GG server:"), 
			                                       wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                       wx.Alignment.wxALIGN_RIGHT);
			serverAddressST = new wx.StaticText(this, Catalog.GetString("GG server address:"));
			serverAddressTC = new wx.TextCtrl(this, (int) Id.serverAddressTC, string.Empty);
			connectionTimeoutST = new wx.StaticText(this, Catalog.GetString("Connection timeout [ms]:"));
			connectionTimeoutSpin = new wx.SpinCtrl(this, (int) Id.connectionTimeoutSpin, string.Empty,
			                                        wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize, 
			                                        wx.SpinCtrl.wxSP_ARROW_KEYS|wx.SpinCtrl.wxSP_WRAP, 
			                                        100, 15000, 2000);
			accountInfoSB = new wx.StaticBox(this, Catalog.GetString("Account info:"));
			statusSB = new wx.StaticBox(this, Catalog.GetString("Status:"));
			connectionSB = new wx.StaticBox(this, Catalog.GetString("Connection:"));
		}
		
		private void DoControlsProperites()
		{
			string[] statuses = { Catalog.GetString("Available"), Catalog.GetString("Busy"), Catalog.GetString("Invisible"), Catalog.GetString("Not available") };
			defaultStatusCombo.Append(statuses);	
		}
		
		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				wx.BoxSizer boxSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
				{
					#region account info
					accountInfoBox = new wx.StaticBoxSizer(accountInfoSB, wx.Orientation.wxVERTICAL);
					{
						accountInfoGrid = new wx.FlexGridSizer(2, 2, 5, 5);
						{
							accountInfoGrid.Add(uinST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							accountInfoGrid.Add(uinTC, 0, wx.Stretch.wxEXPAND);
//							accountInfoGrid.Add(passwordST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
//							accountInfoGrid.Add(passwordTC, 0, wx.Stretch.wxEXPAND);
							accountInfoGrid.Add(nameST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							accountInfoGrid.Add(nameTC, 0, wx.Stretch.wxEXPAND);
						}
						accountInfoGrid.AddGrowableCol(1);
						accountInfoBox.Add(accountInfoGrid, 1, wx.Stretch.wxEXPAND);		
					}
					boxSizer.Add(accountInfoBox, 1, wx.Stretch.wxEXPAND);
					#endregion
									
					#region status				
					statusBox = new wx.StaticBoxSizer(statusSB, wx.Orientation.wxVERTICAL);
					{
						statusGrid = new wx.FlexGridSizer(2, 2, 5, 5);
						{
							statusGrid.Add(defaultStatusST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							statusGrid.Add(defaultStatusCombo, 0, 0);
							statusGrid.Add(saveDescriptionCheck, 0, 0);
						}
						statusGrid.AddGrowableCol(1);
						statusBox.Add(statusGrid, 0, 0);
					}
					boxSizer.Add(statusBox, 1, wx.Stretch.wxEXPAND);
					#endregion
					
					#region connection
					connectionBox = new wx.StaticBoxSizer(connectionSB, wx.Orientation.wxVERTICAL);
					{
						connectionGrid = new wx.FlexGridSizer(3, 2, 5, 5);
						{
							connectionGrid.Add(automaticServerCheck, 0, 0);
							connectionGrid.Add(20, 20, 1, 0, 0);
							connectionGrid.Add(serverAddressST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							connectionGrid.Add(serverAddressTC, 0, wx.Stretch.wxEXPAND);
							connectionGrid.Add(connectionTimeoutST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							connectionGrid.Add(connectionTimeoutSpin, 0, 0);
						}
						connectionGrid.AddGrowableCol(1);
						connectionBox.Add(connectionGrid, 1, wx.Stretch.wxEXPAND);
					}
					boxSizer.Add(connectionBox, 1, wx.Stretch.wxEXPAND);
					#endregion
				}
				topSizer.Add(boxSizer, 1, wx.Stretch.wxEXPAND|wx.Direction.wxALL, 5);
			}
			this.Sizer = topSizer;
			topSizer.SetSizeHints(this);
		}
		
		private void OnAutoServerAddressChange(object sender, wx.Event evt)
		{
			wx.UpdateUIEvent ev = (wx.UpdateUIEvent) evt;
			
			if (automaticServerCheck.Value == true)
				ev.Enabled = false;
			else
				ev.Enabled = true;
		}
		
		public string Uin
		{
			get { return uinTC.Value; }
			set { uinTC.Value = value; }
		}
		
//		public string Password
//		{
//			get { return passwordTC.Value; }
//			set { passwordTC.Value = value; }
//		}
		
		public string Name
		{
			get { return nameTC.Value; }
			set { nameTC.Value = value; }
		}
		
		public int DefaultStatusSelection
		{
			get { return defaultStatusCombo.Selection; }
			set
			{
				if (value >= 0 && value < defaultStatusCombo.Count)
					defaultStatusCombo.Select(value);
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					defaultStatusCombo.Select(0);	
				}
			}
		}
		
		public bool SaveDescriptionsCheck
		{
			get { return saveDescriptionCheck.Value; }
			set { saveDescriptionCheck.Value = value; }
		}
		
		public bool AutoSelectServerCheck
		{
			get { return automaticServerCheck.Value; }
			set { automaticServerCheck.Value = value; }
		}
		
		public string GGServerAddress
		{
			get { return serverAddressTC.Value; }
			set { serverAddressTC.Value = value; }
		}
		
		public int ConnectionTimeout
		{
			get { return connectionTimeoutSpin.Value; }
			set 
			{ 
				if (value > 100)
					connectionTimeoutSpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					connectionTimeoutSpin.Value = 100;
				}
			}
		}
	}
}
